package androidx.preference;

import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.m;
import androidx.fragment.app.u;
import androidx.preference.DialogPreference;
import androidx.preference.PreferenceGroup;
import androidx.preference.j;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public abstract class g extends Fragment implements j.c, j.a, j.b, DialogPreference.a {
    public static final String ARG_PREFERENCE_ROOT = "androidx.preference.PreferenceFragmentCompat.PREFERENCE_ROOT";
    private static final String DIALOG_FRAGMENT_TAG = "androidx.preference.PreferenceFragment.DIALOG";
    private static final int MSG_BIND_PREFERENCES = 1;
    private static final String PREFERENCES_TAG = "android:preferences";
    private static final String TAG = "PreferenceFragment";
    private final d mDividerDecoration = new d();
    private Handler mHandler = new a();
    private boolean mHavePrefs;
    private boolean mInitDone;
    private int mLayoutResId = q.preference_list_fragment;
    RecyclerView mList;
    private j mPreferenceManager;
    private final Runnable mRequestFocus = new b();
    private Runnable mSelectPreferenceRunnable;

    class a extends Handler {
        a() {
        }

        public void handleMessage(Message message) {
            if (message.what == 1) {
                g.this.bindPreferences();
            }
        }
    }

    class b implements Runnable {
        b() {
        }

        public void run() {
            RecyclerView recyclerView = g.this.mList;
            recyclerView.focusableViewAvailable(recyclerView);
        }
    }

    class c implements Runnable {

        /* renamed from: b  reason: collision with root package name */
        final /* synthetic */ Preference f801b;

        /* renamed from: c  reason: collision with root package name */
        final /* synthetic */ String f802c;

        c(Preference preference, String str) {
            this.f801b = preference;
            this.f802c = str;
        }

        public void run() {
            RecyclerView.g adapter = g.this.mList.getAdapter();
            if (adapter instanceof PreferenceGroup.c) {
                Preference preference = this.f801b;
                int d2 = preference != null ? ((PreferenceGroup.c) adapter).d(preference) : ((PreferenceGroup.c) adapter).c(this.f802c);
                if (d2 != -1) {
                    g.this.mList.scrollToPosition(d2);
                } else {
                    adapter.A(new h(adapter, g.this.mList, this.f801b, this.f802c));
                }
            } else if (adapter != null) {
                throw new IllegalStateException("Adapter must implement PreferencePositionCallback");
            }
        }
    }

    private class d extends RecyclerView.n {

        /* renamed from: a  reason: collision with root package name */
        private Drawable f804a;

        /* renamed from: b  reason: collision with root package name */
        private int f805b;

        /* renamed from: c  reason: collision with root package name */
        private boolean f806c = true;

        d() {
        }

        private boolean m(View view, RecyclerView recyclerView) {
            RecyclerView.d0 childViewHolder = recyclerView.getChildViewHolder(view);
            boolean z = false;
            if (!((childViewHolder instanceof l) && ((l) childViewHolder).O())) {
                return false;
            }
            boolean z2 = this.f806c;
            int indexOfChild = recyclerView.indexOfChild(view);
            if (indexOfChild >= recyclerView.getChildCount() - 1) {
                return z2;
            }
            RecyclerView.d0 childViewHolder2 = recyclerView.getChildViewHolder(recyclerView.getChildAt(indexOfChild + 1));
            if ((childViewHolder2 instanceof l) && ((l) childViewHolder2).N()) {
                z = true;
            }
            return z;
        }

        public void e(Rect rect, View view, RecyclerView recyclerView, RecyclerView.a0 a0Var) {
            if (m(view, recyclerView)) {
                rect.bottom = this.f805b;
            }
        }

        public void i(Canvas canvas, RecyclerView recyclerView, RecyclerView.a0 a0Var) {
            if (this.f804a != null) {
                int childCount = recyclerView.getChildCount();
                int width = recyclerView.getWidth();
                for (int i = 0; i < childCount; i++) {
                    View childAt = recyclerView.getChildAt(i);
                    if (m(childAt, recyclerView)) {
                        int y = ((int) childAt.getY()) + childAt.getHeight();
                        this.f804a.setBounds(0, y, width, this.f805b + y);
                        this.f804a.draw(canvas);
                    }
                }
            }
        }

        public void j(boolean z) {
            this.f806c = z;
        }

        public void k(Drawable drawable) {
            this.f805b = drawable != null ? drawable.getIntrinsicHeight() : 0;
            this.f804a = drawable;
            g.this.mList.invalidateItemDecorations();
        }

        public void l(int i) {
            this.f805b = i;
            g.this.mList.invalidateItemDecorations();
        }
    }

    public interface e {
        boolean a(g gVar, Preference preference);
    }

    public interface f {
        boolean a(g gVar, Preference preference);
    }

    /* renamed from: androidx.preference.g$g  reason: collision with other inner class name */
    public interface C0022g {
        boolean a(g gVar, PreferenceScreen preferenceScreen);
    }

    private static class h extends RecyclerView.i {

        /* renamed from: a  reason: collision with root package name */
        private final RecyclerView.g f808a;

        /* renamed from: b  reason: collision with root package name */
        private final RecyclerView f809b;

        /* renamed from: c  reason: collision with root package name */
        private final Preference f810c;

        /* renamed from: d  reason: collision with root package name */
        private final String f811d;

        public h(RecyclerView.g gVar, RecyclerView recyclerView, Preference preference, String str) {
            this.f808a = gVar;
            this.f809b = recyclerView;
            this.f810c = preference;
            this.f811d = str;
        }

        private void g() {
            this.f808a.C(this);
            Preference preference = this.f810c;
            int d2 = preference != null ? ((PreferenceGroup.c) this.f808a).d(preference) : ((PreferenceGroup.c) this.f808a).c(this.f811d);
            if (d2 != -1) {
                this.f809b.scrollToPosition(d2);
            }
        }

        public void a() {
            g();
        }

        public void b(int i, int i2) {
            g();
        }

        public void c(int i, int i2, Object obj) {
            g();
        }

        public void d(int i, int i2) {
            g();
        }

        public void e(int i, int i2, int i3) {
            g();
        }

        public void f(int i, int i2) {
            g();
        }
    }

    private void postBindPreferences() {
        if (!this.mHandler.hasMessages(1)) {
            this.mHandler.obtainMessage(1).sendToTarget();
        }
    }

    private void requirePreferenceManager() {
        if (this.mPreferenceManager == null) {
            throw new RuntimeException("This should be called after super.onCreate.");
        }
    }

    private void scrollToPreferenceInternal(Preference preference, String str) {
        c cVar = new c(preference, str);
        if (this.mList == null) {
            this.mSelectPreferenceRunnable = cVar;
        } else {
            cVar.run();
        }
    }

    private void unbindPreferences() {
        getListView().setAdapter((RecyclerView.g) null);
        PreferenceScreen preferenceScreen = getPreferenceScreen();
        if (preferenceScreen != null) {
            preferenceScreen.S();
        }
        onUnbindPreferences();
    }

    public void addPreferencesFromResource(int i) {
        requirePreferenceManager();
        setPreferenceScreen(this.mPreferenceManager.k(getContext(), i, getPreferenceScreen()));
    }

    /* access modifiers changed from: package-private */
    public void bindPreferences() {
        PreferenceScreen preferenceScreen = getPreferenceScreen();
        if (preferenceScreen != null) {
            getListView().setAdapter(onCreateAdapter(preferenceScreen));
            preferenceScreen.M();
        }
        onBindPreferences();
    }

    public <T extends Preference> T findPreference(CharSequence charSequence) {
        j jVar = this.mPreferenceManager;
        if (jVar == null) {
            return null;
        }
        return jVar.a(charSequence);
    }

    public Fragment getCallbackFragment() {
        return null;
    }

    public final RecyclerView getListView() {
        return this.mList;
    }

    public j getPreferenceManager() {
        return this.mPreferenceManager;
    }

    public PreferenceScreen getPreferenceScreen() {
        return this.mPreferenceManager.i();
    }

    /* access modifiers changed from: protected */
    public void onBindPreferences() {
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        TypedValue typedValue = new TypedValue();
        getActivity().getTheme().resolveAttribute(m.preferenceTheme, typedValue, true);
        int i = typedValue.resourceId;
        if (i == 0) {
            i = s.PreferenceThemeOverlay;
        }
        getActivity().getTheme().applyStyle(i, false);
        j jVar = new j(getContext());
        this.mPreferenceManager = jVar;
        jVar.n(this);
        onCreatePreferences(bundle, getArguments() != null ? getArguments().getString(ARG_PREFERENCE_ROOT) : null);
    }

    /* access modifiers changed from: protected */
    public RecyclerView.g onCreateAdapter(PreferenceScreen preferenceScreen) {
        return new h(preferenceScreen);
    }

    public RecyclerView.o onCreateLayoutManager() {
        return new LinearLayoutManager(getContext());
    }

    public abstract void onCreatePreferences(Bundle bundle, String str);

    public RecyclerView onCreateRecyclerView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        RecyclerView recyclerView;
        if (getContext().getPackageManager().hasSystemFeature("android.hardware.type.automotive") && (recyclerView = (RecyclerView) viewGroup.findViewById(p.recycler_view)) != null) {
            return recyclerView;
        }
        RecyclerView recyclerView2 = (RecyclerView) layoutInflater.inflate(q.preference_recyclerview, viewGroup, false);
        recyclerView2.setLayoutManager(onCreateLayoutManager());
        recyclerView2.setAccessibilityDelegateCompat(new k(recyclerView2));
        return recyclerView2;
    }

    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes((AttributeSet) null, t.PreferenceFragmentCompat, m.preferenceFragmentCompatStyle, 0);
        this.mLayoutResId = obtainStyledAttributes.getResourceId(t.PreferenceFragmentCompat_android_layout, this.mLayoutResId);
        Drawable drawable = obtainStyledAttributes.getDrawable(t.PreferenceFragmentCompat_android_divider);
        int dimensionPixelSize = obtainStyledAttributes.getDimensionPixelSize(t.PreferenceFragmentCompat_android_dividerHeight, -1);
        boolean z = obtainStyledAttributes.getBoolean(t.PreferenceFragmentCompat_allowDividerAfterLastItem, true);
        obtainStyledAttributes.recycle();
        LayoutInflater cloneInContext = layoutInflater.cloneInContext(getContext());
        View inflate = cloneInContext.inflate(this.mLayoutResId, viewGroup, false);
        View findViewById = inflate.findViewById(16908351);
        if (findViewById instanceof ViewGroup) {
            ViewGroup viewGroup2 = (ViewGroup) findViewById;
            RecyclerView onCreateRecyclerView = onCreateRecyclerView(cloneInContext, viewGroup2, bundle);
            if (onCreateRecyclerView != null) {
                this.mList = onCreateRecyclerView;
                onCreateRecyclerView.addItemDecoration(this.mDividerDecoration);
                setDivider(drawable);
                if (dimensionPixelSize != -1) {
                    setDividerHeight(dimensionPixelSize);
                }
                this.mDividerDecoration.j(z);
                if (this.mList.getParent() == null) {
                    viewGroup2.addView(this.mList);
                }
                this.mHandler.post(this.mRequestFocus);
                return inflate;
            }
            throw new RuntimeException("Could not create RecyclerView");
        }
        throw new IllegalStateException("Content has view with id attribute 'android.R.id.list_container' that is not a ViewGroup class");
    }

    public void onDestroyView() {
        this.mHandler.removeCallbacks(this.mRequestFocus);
        this.mHandler.removeMessages(1);
        if (this.mHavePrefs) {
            unbindPreferences();
        }
        this.mList = null;
        super.onDestroyView();
    }

    public void onDisplayPreferenceDialog(Preference preference) {
        androidx.fragment.app.c cVar;
        boolean a2 = getCallbackFragment() instanceof e ? ((e) getCallbackFragment()).a(this, preference) : false;
        if (!a2 && (getActivity() instanceof e)) {
            a2 = ((e) getActivity()).a(this, preference);
        }
        if (!a2 && getParentFragmentManager().X(DIALOG_FRAGMENT_TAG) == null) {
            if (preference instanceof EditTextPreference) {
                cVar = a.t(preference.o());
            } else if (preference instanceof ListPreference) {
                cVar = c.t(preference.o());
            } else if (preference instanceof MultiSelectListPreference) {
                cVar = d.t(preference.o());
            } else {
                throw new IllegalArgumentException("Cannot display dialog for an unknown Preference type: " + preference.getClass().getSimpleName() + ". Make sure to implement onPreferenceDisplayDialog() to handle displaying a custom dialog for this Preference.");
            }
            cVar.setTargetFragment(this, 0);
            cVar.k(getParentFragmentManager(), DIALOG_FRAGMENT_TAG);
        }
    }

    public void onNavigateToScreen(PreferenceScreen preferenceScreen) {
        if (!(getCallbackFragment() instanceof C0022g ? ((C0022g) getCallbackFragment()).a(this, preferenceScreen) : false) && (getActivity() instanceof C0022g)) {
            ((C0022g) getActivity()).a(this, preferenceScreen);
        }
    }

    public boolean onPreferenceTreeClick(Preference preference) {
        if (preference.l() == null) {
            return false;
        }
        boolean a2 = getCallbackFragment() instanceof f ? ((f) getCallbackFragment()).a(this, preference) : false;
        if (!a2 && (getActivity() instanceof f)) {
            a2 = ((f) getActivity()).a(this, preference);
        }
        if (a2) {
            return true;
        }
        Log.w(TAG, "onPreferenceStartFragment is not implemented in the parent activity - attempting to use a fallback implementation. You should implement this method so that you can configure the new fragment that will be displayed, and set a transition between the fragments.");
        m m = requireActivity().m();
        Bundle j = preference.j();
        Fragment a3 = m.e0().a(requireActivity().getClassLoader(), preference.l());
        a3.setArguments(j);
        a3.setTargetFragment(this, 0);
        u i = m.i();
        i.p(((View) getView().getParent()).getId(), a3);
        i.f((String) null);
        i.h();
        return true;
    }

    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        PreferenceScreen preferenceScreen = getPreferenceScreen();
        if (preferenceScreen != null) {
            Bundle bundle2 = new Bundle();
            preferenceScreen.k0(bundle2);
            bundle.putBundle(PREFERENCES_TAG, bundle2);
        }
    }

    public void onStart() {
        super.onStart();
        this.mPreferenceManager.o(this);
        this.mPreferenceManager.m(this);
    }

    public void onStop() {
        super.onStop();
        this.mPreferenceManager.o((j.c) null);
        this.mPreferenceManager.m((j.a) null);
    }

    /* access modifiers changed from: protected */
    public void onUnbindPreferences() {
    }

    public void onViewCreated(View view, Bundle bundle) {
        Bundle bundle2;
        PreferenceScreen preferenceScreen;
        super.onViewCreated(view, bundle);
        if (!(bundle == null || (bundle2 = bundle.getBundle(PREFERENCES_TAG)) == null || (preferenceScreen = getPreferenceScreen()) == null)) {
            preferenceScreen.j0(bundle2);
        }
        if (this.mHavePrefs) {
            bindPreferences();
            Runnable runnable = this.mSelectPreferenceRunnable;
            if (runnable != null) {
                runnable.run();
                this.mSelectPreferenceRunnable = null;
            }
        }
        this.mInitDone = true;
    }

    public void scrollToPreference(Preference preference) {
        scrollToPreferenceInternal(preference, (String) null);
    }

    public void scrollToPreference(String str) {
        scrollToPreferenceInternal((Preference) null, str);
    }

    public void setDivider(Drawable drawable) {
        this.mDividerDecoration.k(drawable);
    }

    public void setDividerHeight(int i) {
        this.mDividerDecoration.l(i);
    }

    public void setPreferenceScreen(PreferenceScreen preferenceScreen) {
        if (this.mPreferenceManager.p(preferenceScreen) && preferenceScreen != null) {
            onUnbindPreferences();
            this.mHavePrefs = true;
            if (this.mInitDone) {
                postBindPreferences();
            }
        }
    }

    public void setPreferencesFromResource(int i, String str) {
        requirePreferenceManager();
        PreferenceScreen k = this.mPreferenceManager.k(getContext(), i, (PreferenceScreen) null);
        Object obj = k;
        if (str != null) {
            Object H0 = k.H0(str);
            boolean z = H0 instanceof PreferenceScreen;
            obj = H0;
            if (!z) {
                throw new IllegalArgumentException("Preference object with key " + str + " is not a PreferenceScreen");
            }
        }
        setPreferenceScreen((PreferenceScreen) obj);
    }
}
